package com.jbh.fourclibrary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jbh.fourclibrary.R;
import com.jbh.fourclibrary.activity.PlayMp4Activity;
import com.jbh.fourclibrary.util.Util;
import com.jbh.fourclibrary.view.VerticalSeekBar1;


public class CaratFragment extends Fragment {
    TextView tv_ct_val;
    ImageView imgTry;
    View carat_view;
    public CaratFragment() { }
    public VerticalSeekBar1 mVerticalSeekBar ;
    private String[] carat = {"0.3ct",  "0.5ct", "0.7ct","0.9ct", "1.5ct", "2.0ct", "3.0ct", "4.0ct", "5.0ct",};

    public static CaratFragment newInstance() {
        CaratFragment fragment = new CaratFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_carat, container, false);
        initView(view);
        return view;
    }




    /**
     * @param view
     */
    private void initView(View view) {
        carat_view = view.findViewById(R.id.carat_view);
        measure(view);
        tv_ct_val = view.findViewById(R.id.tv_ct_val);
        mVerticalSeekBar = view.findViewById(R.id.mVerticalSeekBar);
        mVerticalSeekBar.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener());
        imgTry=view.findViewById(R.id.imgTry);
        view.findViewById(R.id.tv_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), PlayMp4Activity.class)
                        .putExtra("av_name","av_carat"));
            }
        });
    }

    private void measure(View view) {
        int height = Util.getHeight(getActivity())
                - Util.getStatusBarHeight(getActivity())
                - Util.dpToPx(getActivity(),90);
        ViewGroup.LayoutParams params = carat_view.getLayoutParams();
        params.height = height;


        ImageView img1 = view.findViewById(R.id.img1);
        params = img1.getLayoutParams();
        params.width = height - Util.dpToPx(getActivity(),110);
    }


    public class MyOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            int height = mVerticalSeekBar.getMeasuredHeight()-Util.dpToPx(getActivity(),30);
            float scale = (float) (1.0*progress/100);

            tv_ct_val.setTranslationY(scale*height);

            setVal(scale);


            imgTry.setRotation((float) (scale*720));
            scale = (float) (0.3f+ scale*(1-0.6));
            imgTry.setScaleY(scale);
            imgTry.setScaleX(scale);
        }


        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    }


    /**
     * @param scale
     */
    private void setVal(float scale) {
        float radio = scale * 8;
        if (radio>=0.5&&radio<1){
            tv_ct_val.setText("0.4ct");
        }else if (radio>=1.5&&radio<2){
            tv_ct_val.setText("0.6ct");
        }else if (radio>=2.5&&radio<3){
            tv_ct_val.setText("0.8ct");
        }else if (radio>=3&&radio<4){
            if (radio>=3){
                tv_ct_val.setText("0.9ct");
            }
            if (radio>=3.1){
                tv_ct_val.setText("1.0ct");
            }
            if (radio>=3.2){
                tv_ct_val.setText("1.1ct");
            }
            if (radio>=3.4){
                tv_ct_val.setText("1.2ct");
            }
            if (radio>=3.6){
                tv_ct_val.setText("1.3ct");
            }
            if (radio>=3.8){
                tv_ct_val.setText("1.4ct");
            }
        }else if (radio>=4&&radio<5){
            if (radio>=4){
                tv_ct_val.setText("1.5ct");
            }
            if (radio>=4.2){
                tv_ct_val.setText("1.6ct");
            }
            if (radio>=4.4){
                tv_ct_val.setText("1.7ct");
            }
            if (radio>=4.6){
                tv_ct_val.setText("1.8ct");
            }
            if (radio>=4.8){
                tv_ct_val.setText("1.9ct");
            }
        }else if(radio>=5&&radio<6){
            if (radio>=5){
                tv_ct_val.setText("2.0ct");
            }
            if (radio>=5.1){
                tv_ct_val.setText("2.1ct");
            }
            if (radio>=5.2){
                tv_ct_val.setText("2.2ct");
            }
            if (radio>=5.3){
                tv_ct_val.setText("2.3ct");
            }
            if (radio>=5.4){
                tv_ct_val.setText("2.4ct");
            }
            if (radio>=5.5){
                tv_ct_val.setText("2.5ct");
            }
            if (radio>=5.6){
                tv_ct_val.setText("2.6ct");
            }
            if (radio>=5.7){
                tv_ct_val.setText("2.7ct");
            }
            if (radio>=5.8){
                tv_ct_val.setText("2.8ct");
            }
            if (radio>=5.9){
                tv_ct_val.setText("2.9ct");
            }
        } else if (radio>=6&&radio<7){
            if (radio>=6){
                tv_ct_val.setText("3.0ct");
            }
            if (radio>=6.1){
                tv_ct_val.setText("3.1ct");
            }
            if (radio>=6.2){
                tv_ct_val.setText("3.2ct");
            }
            if (radio>=6.3){
                tv_ct_val.setText("3.3ct");
            }
            if (radio>=6.4){
                tv_ct_val.setText("3.4ct");
            }
            if (radio>=6.5){
                tv_ct_val.setText("3.5ct");
            }
            if (radio>=6.6){
                tv_ct_val.setText("3.6ct");
            }
            if (radio>=6.7){
                tv_ct_val.setText("3.7ct");
            }
            if (radio>=6.8){
                tv_ct_val.setText("3.8ct");
            }
            if (radio>=6.9){
                tv_ct_val.setText("3.9ct");
            }
        }else if (radio>=7&&radio<8){
            if (radio>=7){
                tv_ct_val.setText("4.0ct");
            }
            if (radio>=7.1){
                tv_ct_val.setText("4.1ct");
            }
            if (radio>=7.2){
                tv_ct_val.setText("4.2ct");
            }
            if (radio>=7.3){
                tv_ct_val.setText("4.3ct");
            }
            if (radio>=7.4){
                tv_ct_val.setText("4.4ct");
            }
            if (radio>=7.5){
                tv_ct_val.setText("4.5ct");
            }
            if (radio>=7.6){
                tv_ct_val.setText("4.6ct");
            }
            if (radio>=7.7){
                tv_ct_val.setText("4.7ct");
            }
            if (radio>=7.8){
                tv_ct_val.setText("4.8ct");
            }
            if (radio>=7.9){
                tv_ct_val.setText("4.9ct");
            }
        }else {
            /*当前钻石大小*/
            int position = (int) Math.floor(radio);
            tv_ct_val.setText(carat[position]);
        }
    }
}
